package com.kaibes.core.base.reflection;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

public class ClassReflection {

    private Class<? extends Object> clazz;
    private String name;
    
    private String fieldNameString;
    private String[] fieldNameArr;
    private String allFieldNameString;
    private String[] allFieldNameArr;
    
    private HashMap<String, Field> fieldMap;
    private HashMap<String, Method> getMethodMap;
    private HashMap<String, Method> setMethodMap;
    
    
    public ClassReflection(Class<? extends Object> clazz) {
        this.clazz = clazz;
        fieldMap = new HashMap<>();
        getMethodMap = new HashMap<>();
        setMethodMap = new HashMap<>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Class<? extends Object> getClazz() {
        return clazz;
    }
    
    public void setFieldNameString(String fieldNameString) {
        this.fieldNameString = fieldNameString;
    }
    
    public String getFieldNameString() {
        return fieldNameString;
    }

    public void setFieldNameArr(String[] fieldNameArr) {
        this.fieldNameArr = fieldNameArr;
    }
    
    public String[] getFieldNameArr() {
        return fieldNameArr;
    }

    public String getAllFieldNameString() {
        return allFieldNameString;
    }

    public void setAllFieldNameString(String allFieldNameString) {
        this.allFieldNameString = allFieldNameString;
    }

    public String[] getAllFieldNameArr() {
        return allFieldNameArr;
    }

    public void setAllFieldNameArr(String[] allFieldNameArr) {
        this.allFieldNameArr = allFieldNameArr;
    }

    public HashMap<String, Field> getFieldMap() {
        return fieldMap;
    }

    public HashMap<String, Method> getGetMethodMap() {
        return getMethodMap;
    }

    public HashMap<String, Method> getSetMethodMap() {
        return setMethodMap;
    }
    
    public Object getValue(Object instance, String fieldName) {
        try {
            return getMethodMap.get(fieldName).invoke(instance);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public void setValue(Object instance, String fieldName, Object value) {
        try {
            setMethodMap.get(fieldName).invoke(instance, value);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    
}
